package com.yanqu.road.server.timer;

import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.List;

public class MgrDataSaveTask extends GeneralTask {

    private static int saveMgrThreadCount = Runtime.getRuntime().availableProcessors();

    private int saveIndex;

    public MgrDataSaveTask(int saveIndex) {
        this.saveIndex = saveIndex;
    }

    @Override
    public void doRun() {
        List<TempMgr> mgrList = GameServer.getInstance().getMgrList();
        for (int index = 0; index < mgrList.size(); index++) {
            if (mgrList.get(index) != null && index % saveMgrThreadCount == saveIndex) {
                long begin = System.currentTimeMillis();
                TempMgr mgr = mgrList.get(index);
                try {
                    mgr.save();
                } catch (Exception ex) {
                    getLOGGER().error("save {} err. {}", mgr.getClass().getSimpleName(), ex);
                }

                long cost = System.currentTimeMillis() - begin;
                if (cost > 1000) {
                    getLOGGER().info("save {}  cost {} ms", mgr.getClass().getSimpleName(), cost);
                }
            }
        }
    }
}
